﻿using MapleLeaf.Core.Dto;
using MapleLeaf.DataBase.Repository;
using MapleLeaf.Model.CookBook.Organization.Entity;
using MapleLeaf.Model.CookBook.Organization.Relation;
using MapleLeaf.Service.CookBook.Interface;
using MapleLeaf.Service.CookBook.Models;
using SqlSugar;

namespace MapleLeaf.Service.CookBook.Organization {
    /// <summary>
    /// 
    /// <inheritdoc cref="ISysUserService"/>
    /// </summary>
    public class SysUserService(ISqlSugarClient db) : DataBaseRepository<UserEntity>(db), IScopedService {
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public async Task<UserInfo> CheckUserInfoByAccount(string account, string password) {
            var user = await Context.Queryable<UserEntity>()
                                                    .Where(p => p.UserName == account && p.Password == password)
                                                    .Select(p => new UserInfo {
                                                        Id = p.Id,
                                                        NickName = p.NickName,
                                                        UserName = p.UserName,
                                                        OrgId = p.DeptId,
                                                        Avatar = p.Avatar
                                                    })
                                                    .FirstAsync();
            return user;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task ResetPasswordAsync(List<long> ids) {
            await Context.Updateable<UserEntity>()
                                    .SetColumns(x => x.Password == "888888")
                                    .Where(it => ids.Contains(it.Id))
                                    .ExecuteCommandAsync();

        }
        /// <summary>
        /// 获取个人资料
        /// </summary> 
        /// <returns></returns>
        public async Task<UserProfileModel> GetUserProfileModelAsync(string account) {
            var model = await Context.Queryable<UserEntity>()
                                     .LeftJoin<UserRoleEntity>((u, rel) => u.Id == rel.UserId)
                                     .LeftJoin<RoleEntity>((u, rel, r) => rel.RoleId == r.Id)
                                     .LeftJoin<DepartmentEntity>((u, rel, r, d) => u.DeptId == d.Id)
                                .Where((u, rel, r, d) => u.UserName == account)
                                .Select((u, rel, r, d) => new UserProfileModel {
                                    Id = u.Id,
                                    UserName = u.UserName,
                                    NickName = u.NickName,
                                    Email = u.Email,
                                    Avatar = u.Avatar,
                                    RoleNames = r.Name,
                                    DeptName = d.Name,
                                    Mobile = u.Telephone,
                                    Gender = u.Gender,
                                    CreateTime = u.CreateTime,

                                }).FirstAsync();
                                                    
            return model;
        }

    }

}
